﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Web.Report.SQLReport;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using Kingdee.BOS.WebApi.FormService;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace RK.ReportPlugin.BPW
{
    [Description("SQL报表选择行进行审核"), HotUpdate]
    public class SelectSqlReportToDo : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            if (e.BarItemKey.EqualsIgnoreCase("btnExportCustomer"))
            {
                //获取报表左侧勾选的选择数据
                var selData = (this.View as SQLReportView).SelectedDataRows;
                //遍历DataRows[]数组，获取选择行数据
                if (selData == null)
                {
                    this.View.ShowErrMessage("没有选择数据，请确认！");
                    return;
                }
                List<string> msg = new List<string>();
                foreach (var row in selData)
                {
                    var billno = row[1].ToString();
                    var status = row[2].ToString();
                    if (status == "审核中")
                    {
                        //审核对应销售订单
                        if (AuditSalOrder(billno))
                        {
                            msg.Add($"订单：{billno},审核成功!");
                        }
                        else
                        {
                            msg.Add($"订单：{billno},审核失败!");
                        }
                    }
                    else
                    {
                        this.View.ShowErrMessage("选择的行数据还未提交，不可以审核！");
                        return;
                    }
                }
                if (msg.Count() > 0)
                {
                    string msgStr = string.Empty;
                    foreach (var item in msg)
                    {
                        msgStr += item;
                    }
                    this.View.Refresh();
                    this.View.ShowMessage(msgStr);
                }
            }

        }

        private bool AuditSalOrder(string billno)
        {
            bool isOk = false;
            string formId = "SAL_SaleOrder";
            var result = WebApiServiceCall.ExecuteBillQuery(this.Context, Base.QueryParameter(formId, "FDocumentStatus", $"fbillno='{billno}'"));
            string status = result[0][0].ToString();
            switch (status)
            {
                case "A":
                case "D":
                    var opResult = JsonConvert.SerializeObject(WebApiServiceCall.Submit(this.Context, formId, Base.GetParam(billno)));
                    JObject saveResult = JObject.Parse(opResult);
                    var isSuccess = saveResult["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>();
                    if (isSuccess)
                    {
                        var auditResult = JsonConvert.SerializeObject(WebApiServiceCall.Audit(this.Context, formId, Base.GetParam(billno)));
                        JObject aResult = JObject.Parse(auditResult);
                        isOk = aResult["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>();
                        //if (aSuccess)
                        //{
                        //    upSalDateAndRemark(rows);
                        //}
                    }
                    break;
                case "B":
                    var bResult = JsonConvert.SerializeObject(WebApiServiceCall.Audit(this.Context, formId, Base.GetParam(billno)));
                    JObject bObj = JObject.Parse(bResult);
                    isOk = bObj["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>();
                    //if (bSuccess)
                    //{
                    //    upSalDateAndRemark(rows);
                    //}
                    break;
                default:
                    //upSalDateAndRemark(rows);
                    break;
            }
            return isOk;
        }
    }
}
